github_qr_code()
submit choice Burj Khalifa - Dubai¶opt-in¶App.box_*() APIApp.box_create(name, size) - any size ($\leq$ 32KB)¶App.box_length(name) - opcode box_len¶App.box_delete(name) - opcode box_del¶App.box_replace(name, idx, L) - set part of box¶App.box_extract(name, idx, L) - get part of box¶App.box_put(name, value) - set value (may create box)¶App.box_get(name) - get everything (fail if size > 4KB)¶App.box_get() and App.box_put()¶from pyteal import Itob, Int, Seq, App, Txn, If
# App.box: account --> choice
# App.global: choice --> count
# for real app, choice is passed in as parameter.
choice = Itob(Int(1)) # 1: index of "Burj Khalifa - Dubai"
submit_expr = Seq( # switch vote old_choice ---> choice
old_choice := App.box_get(Txn.sender()), # old_choice: MaybeValue
If(old_choice.hasValue()).Then(
App.globalPut( # App.global[old_choice] -= 1
old_choice.value(),
App.globalGet(old_choice.value()) - Int(1),
),
),
App.box_put(Txn.sender(), choice), # App.box[account] <--- choice
App.globalPut( # App.global[choice] += 1
choice,
App.globalGet(choice) + Int(1),
),
)
methods - allow interacting with Poll App¶delete for reasons to be explained shortly)¶OnComplete Actions for App Transactions¶Router and its M.O.E. Questions¶moe()
Router Compilation into an ABI Smart Contract¶Router Initialization¶warning()
# WARNING: STUBS ARE FOR ROUTER-ILLUSTRATION PURPOSES ONLY!!!
del_action = OnCompleteAction.call_only(Seq())
router = Router(
name="OpenPollingApp",
descr="This is a polling application.",
bare_calls=BareCallActions(delete_application=del_action),
)
methods with @router.method decorator¶@router.method
def submit(choice: abi.Uint8) -> Expr:
"""Submit a response to the poll.""" # <-- Docstring
return Seq()
opts = OptimizeOptions(scratch_slots=True)
(approval, clear, json_contract) = \
router.compile_program(version=8, optimize=opts)
# ------------------------------------^^^^^^^^^^^^^
print(json.dumps(json_contract.dictify(), indent=2))
{
"name": "OpenPollingApp",
"methods": [
{
"name": "submit",
"args": [
{
"type": "uint8",
"name": "choice"
}
],
"returns": {
"type": "void"
},
"desc": "Submit a response to the poll."
}
],
"networks": {},
"desc": "This is a polling application."
}
method submit(choice: abi.Uint8)¶show() # approval
txna ApplicationArgs 0
method "submit(uint8)void"
==
bnz main_l3
. . .
main_l3:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
txna ApplicationArgs 1
int 0
getbyte
callsub submit_0
int 1
return
. . .
// submit
submit_0:
store 0
retsub
bare call for delete¶show() # approval
txn NumAppArgs
int 0
==
bnz main_l4
. . .
main_l4:
txn OnCompletion
int DeleteApplication
==
bnz main_l6
. . .
main_l6:
txn ApplicationID
int 0
!=
assert
int 1
return